<?php
/**
 * 用户文件
 *
 * @author lwb
 */
namespace app\wechat\controller;

use think\Log;

class Wechat extends Common {

    public function __construct() {
        parent::__construct();
    }

    public function openId() {
        $request = request();
        $params = $request->param();

        $get_openid_url = "https://api.weixin.qq.com/sns/oauth2/access_token?appid=%s&secret=%s&code=%s&grant_type=authorization_code";
        $get_openid_url = sprintf($get_openid_url, config('wechat.'. APP_ENV)['appid'], config('wechat.'. APP_ENV)['appsecret'], $params['code']);

        // 拿到code取openid
        $res = file_get_contents($get_openid_url);

        $base_info = json_decode($res, true);

        session("openid", $base_info['openid']);
        if(session('redirect_url')){
            return redirect(session('redirect_url'));
        }elseif(session('partner_id')){
            return redirect('/user/register?type=jiaoyu');
        }else{
            return redirect('/register-'.session('phone').'-'.session('b_id'));
        }
    }

    /**
     * 获取微信的openid
     */
    public function getOpenId()
    {
        // 获取code参数
        $params = request()->param();
        $get_openid_url = "https://api.weixin.qq.com/sns/oauth2/access_token?appid=%s&secret=%s&code=%s&grant_type=authorization_code";
        $get_openid_url = sprintf($get_openid_url, config('wechat.'. APP_ENV)['appid'], config('wechat.'. APP_ENV)['appsecret'], $params['code']);

        $res = file_get_contents($get_openid_url);
        $base_info = json_decode($res, true);
//        print_r($base_info);exit;

        header("Location: http://my.admin.mayi121.com/admin/account/recharge_balance.html?openid={$base_info['openid']}");
    }

    /**
     * 获取微信的tokenid
     */
    public function getTokenId()
    {
        // 获取code参数
        $params = request()->param();
        $get_openid_url = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid={$params['appid']}&secret={$params['secret']}";
        $res = file_get_contents($get_openid_url);
	return $res;
    }

    /**
     * 获取微信的tokenid
     */
    public function getToken()
    {
        // 获取code参数
        $params = request()->param();
        $get_openid_url = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid={$params['appid']}&secret={$params['secret']}";
        $res = file_get_contents($get_openid_url);

        header("Location: http://my.admin.mayi121.com/admin/account/recharge_balance.html?token={$res['access_token']}");
//        return $res;
    }

    /**
     * 获取微信的getOrderOpenId
     */
    public function getOrderOpenId()
    {
        // 获取code参数
        $params = request()->param();
        $get_openid_url = "https://api.weixin.qq.com/sns/oauth2/access_token?appid=%s&secret=%s&code=%s&grant_type=authorization_code";
        $get_openid_url = sprintf($get_openid_url, config('wechat.'. APP_ENV)['appid'], config('wechat.'. APP_ENV)['appsecret'], $params['code']);

        $res = file_get_contents($get_openid_url);
        $base_info = json_decode($res, true);

        session("openid", $base_info['openid']);
        session('p_id',$params['p_id']);

//        print_r(session("p_id"));
//        print_r(session("openid"));exit;

	    header("Location: http://release.weixin.mayi121.com/confirmOrder?p_id={$params['p_id']}&openid={$base_info['openid']}");
    }

    /**
     * 首付获取微信的 getProductOpenId
     */
    public function getProductOpenId()
    {
        // 获取code参数
        $params = request()->param();

        $get_openid_url = "https://api.weixin.qq.com/sns/oauth2/access_token?appid=%s&secret=%s&code=%s&grant_type=authorization_code";
        $get_openid_url = sprintf($get_openid_url, config('wechat.'. APP_ENV)['appid'], config('wechat.'. APP_ENV)['appsecret'], $params['code']);

        $res = file_get_contents($get_openid_url);
        $base_info = json_decode($res, true);

        session("openid", $base_info['openid']);

        header("Location: http://release.weixin.mayi121.com/pay/?order_num={$params['order_num']}&openid={$base_info['openid']}");
    }

    //收款通知获取openid
    public function getReceiptOpenId()
    {
        // 获取code参数
        $params = request()->param();

        $get_openid_url = "https://api.weixin.qq.com/sns/oauth2/access_token?appid=%s&secret=%s&code=%s&grant_type=authorization_code";
        $get_openid_url = sprintf($get_openid_url, config('wechat.'. APP_ENV)['appid'], config('wechat.'. APP_ENV)['appsecret'], $params['code']);

        $res = file_get_contents($get_openid_url);
        $base_info = json_decode($res, true);
        Log::write($base_info, '信息0', true, 'weixins/');
        session("openid", $base_info['openid']);

        header("Location: http://release.weixin.mayi121.com/receiptinfo?partner_id={$params['partner_id']}&openid={$base_info['openid']}&access_token={$base_info['access_token']}");
    }


    /**
     * 测试获取微信的 getTestOpenId
     */
    public function getTestOpenId()
    {
        // 获取code参数
        $params = request()->param();

        $get_openid_url = "https://api.weixin.qq.com/sns/oauth2/access_token?appid=%s&secret=%s&code=%s&grant_type=authorization_code";
        $get_openid_url = sprintf($get_openid_url, config('wechat.'. APP_ENV)['appid'], config('wechat.'. APP_ENV)['appsecret'], $params['code']);

        $res = file_get_contents($get_openid_url);
        $base_info = json_decode($res, true);

        session("openid", $base_info['openid']);

        header("Location: http://test.weixin2.mayi121.com/pay/?order_num={$params['order_num']}&openid={$base_info['openid']}");
    }

    /**
     * 测试获取微信的 getTestOpenId
     */
    public function getTestOpenIds()
    {
        // 获取code参数
        $params = request()->param();

        $get_openid_url = "https://api.weixin.qq.com/sns/oauth2/access_token?appid=%s&secret=%s&code=%s&grant_type=authorization_code";
        $get_openid_url = sprintf($get_openid_url, config('wechat.'. APP_ENV)['appid'], config('wechat.'. APP_ENV)['appsecret'], $params['code']);

        $res = file_get_contents($get_openid_url);
        $base_info = json_decode($res, true);

        session("openid", $base_info['openid']);

        header("Location: http://test.weixin2.mayi121.com/payInsurance/?order_num={$params['order_num']}&openid={$base_info['openid']}");
    }

    /**
     * 获取微信分享配置
     */
    public function GetSignPackage()
    {
        $jssdk = new jssdk(config('wechat.app')['appid'], config('wechat.app')['appsecret']);
        $signPackage = $jssdk->GetSignPackage();

        if($signPackage){
            return $this->parse_data(['code' => 2000, 'data' => ['signPackage' => $signPackage]]);
        }else{
            return $this->parse_data(['code' => 5000, 'msg' => '分享调起失败!','closeCurrTab'=>false]);
        }
    }
}
